//
//  PaintingContentCell.swift
//  NDAi
//
//  Created by 丸子 on 2024/6/4.
//

import UIKit
import YYText
class PaintingContentCell: UITableViewCell {
    var chooseDesc:((String) -> Void)?
    var chooseOther:((String) -> Void)?
    
    lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = Theme.color.main
        view.allRound(4)
        return view
    }()
    
    lazy var titleLb: UILabel = {
        let label = UILabel()
        label.font = UIFont.pingFangSCRegular(ofSize: 15)
        label.textColor = Theme.color.main
        label.text = "输入描述词"
        return label
    }()
    
    lazy var descView: UIView = {
        let view = UIView()
        view.allRound(8)
        view.backgroundColor = .white
        return view
    }()
    
    lazy var descTextView: YYTextView = {
        let view = YYTextView()
        view.textContainerInset = UIEdgeInsets(top: 15, left: 15, bottom: 15, right: 15);
        view.placeholderText = "建议使用中文输入，详细描述画面主体、细节、场景等，文本描述越丰富，生成效果越精美"
        view.placeholderFont = UIFont.pingFangSCRegular(ofSize: 12)
        view.placeholderTextColor = Theme.color.text_d3
        view.textColor = Theme.color.text_d1
        view.delegate = self
        view.font = UIFont.pingFangSCRegular(ofSize: 12)
        return view
    }()
    
    lazy var countLb: UILabel = {
        let label = UILabel()
        label.font = UIFont.pingFangSCSemibold(ofSize: 12)
        label.textColor = Theme.color.text_d2
        label.text = "0/100"
        return label
    }()
    
    
//    lazy var descTitleLb: UILabel = {
//        let label = UILabel()
//        label.font = UIFont.pingFangSCRegular(ofSize: 12)
//        label.textColor = Theme.color.main
//        label.text = "输入描述词"
//        return label
//    }()
//    
//    lazy var twoDescLb: UILabel = {
//        let label = UILabel()
//        label.font = UIFont.pingFangSCRegular(ofSize: 11)
//        label.textColor = Theme.color.text_d3
//        label.text = "描述不想出现在画面上的内容"
//        return label
//    }()
//    
//    lazy var switchBtn: UIButton = {
//        let button = UIButton(type: .custom)
//        button.imageForNormal = UIImage(named: "icon_paint_nor")
//        button.imageForSelected = UIImage(named: "icon_paint_sel")
//        return button
//    }()
//    
//    lazy var twoDescView: UIView = {
//        let view = UIView()
//        view.allRound(8)
//        view.backgroundColor = .white
//        view.isHidden = true
//        return view
//    }()
//    
//    lazy var twoDescTextView: YYTextView = {
//        let view = YYTextView()
//        view.textContainerInset = UIEdgeInsets(top: 15, left: 15, bottom: 15, right: 15);
//        view.placeholderText = "请输入不想出现在画面中的内容。"
//        view.placeholderFont = UIFont.pingFangSCRegular(ofSize: 12)
//        view.placeholderTextColor = Theme.color.text_d3
//        view.textColor = Theme.color.text_d1
//        view.delegate = self
//        view.font = UIFont.pingFangSCRegular(ofSize: 12)
//        view.isHidden = true
//        return view
//    }()
//    
//    lazy var twoCountLb: UILabel = {
//        let label = UILabel()
//        label.font = UIFont.pingFangSCSemibold(ofSize: 12)
//        label.textColor = Theme.color.text_d2
//        label.text = "0/50"
//        label.isHidden = true
//        return label
//    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        backgroundColor = .clear
        selectionStyle = .none
            //, descTitleLb, twoDescLb, switchBtn, twoDescView
        contentView.addSubviews([lineView, titleLb, descView])
        descView.addSubviews([descTextView, countLb])
//        twoDescView.addSubviews([twoDescTextView, twoCountLb])
        
        lineView.snp.makeConstraints { make in
            make.top.equalTo(22)
            make.leading.equalTo(16)
            make.height.equalTo(16)
            make.width.equalTo(6)
        }
        
        titleLb.snp.makeConstraints { make in
            make.centerY.equalTo(lineView)
            make.leading.equalTo(lineView.snp.trailing).offset(8)
        }
        
        descView.snp.makeConstraints { make in
            make.top.equalTo(lineView.snp.bottom).offset(12)
            make.leading.trailing.equalToSuperview().inset(16)
            make.height.equalTo(130)
        }
        
        descTextView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        countLb.snp.makeConstraints { make in
            make.trailing.bottom.equalToSuperview().inset(12)
        }
        
//        descTitleLb.snp.makeConstraints { make in
//            make.top.equalTo(descView.snp.bottom).offset(15)
//            make.leading.equalTo(16)
//        }
//        
//        twoDescLb.snp.makeConstraints { make in
//            make.top.equalTo(descTitleLb.snp.bottom).offset(0)
//            make.leading.equalTo(16)
//        }
//        
//        switchBtn.snp.makeConstraints { make in
//            make.top.equalTo(descView.snp.bottom).offset(20)
//            make.trailing.equalToSuperview().offset(-15)
//            make.width.equalTo(50)
//            make.height.equalTo(24)
//        }
//        
//        twoDescView.snp.makeConstraints { make in
//            make.top.equalTo(switchBtn.snp.bottom).offset(15)
//            make.leading.trailing.equalToSuperview().inset(16)
//            make.height.equalTo(84)
//        }
//        
//        twoDescTextView.snp.makeConstraints { make in
//            make.edges.equalToSuperview()
//        }
//        
//        twoCountLb.snp.makeConstraints { make in
//            make.trailing.bottom.equalToSuperview().inset(12)
//        }
        
        
        
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension PaintingContentCell: YYTextViewDelegate {
     
    func textViewDidChange(_ textView: YYTextView) {
        let currentLength = textView.text.count
        if textView == self.descTextView {
            if currentLength > 100 {
                textView.text = String(textView.text.prefix(100))
            }
            self.countLb.text = "\(textView.text.count)/100"
            self.chooseDesc?(textView.text)
        } else {
            if currentLength > 50 {
                textView.text = String(textView.text.prefix(50))
            }
//            self.twoCountLb.text = "\(textView.text.count)/50"
            self.chooseOther?(textView.text)
        }
    }
    func textViewDidEndEditing(_ textView: YYTextView) {
         
    }
   
}
